var ns, toast, actionsheet, UIAlbumBrowser;
var app = new Vue({
    el: '#app',
    data: {
        ID:0,
        list:[],
        ProjectList:[],
        showTime:false,
        youxiaoTime:'',
        Type:1,//1-打折、2-降价
        show_project:false,
        show_pro:false,//打开编辑商品
        editmsg:{},//选中编辑的商品
        ProjectID:0,
        ProjectName:'',
        Name:'',//活动名称
        Discount:'',//折扣比例
        StartTime:'',
        EndTime:'',
    },
    methods: {
        //获取所属项目
        get_project:function(){
            var that=this;
            ns.post({
                action: 'getbusinessproject'
             }, function (succeed, data, err) {
                 if(succeed){
                    that.ProjectList=data.ProjectList;
                    console.log(JSON.stringify(that.ProjectList));
                 }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                 }
                
             });

        },
        doSelectRoom:function(item){
            var that=this;
            that.ProjectID=item.ID;
            that.ProjectName=item.Name;
            that.show_project=false;
        },
        //选择所属项目
        choose_project:function(){
            var that=this;
            that.show_project=true;
        },
        //选择类型1-打折，2-降价
        do_select_type:function(type){
            var that=this;
            that.list=[];
            that.Type=type;
        },

        //添加新活动
        add_product:function(){
            var that=this;
            ns.openWin('choose_product', '选择商品',{
                list:that.list,
                ProductType:'activity',
                Type:that.Type,
            });
        },
        //修改选中商品
        update_pro:function(item){
            var that=this;
            that.show_pro=true;
            that.editmsg=item;
        },
        // 保存修改
        save_editpro:function(){
            var that=this;
            that.show_pro=false;
            
        },
        closebox:function(){
            var that=this;
            that.show_pro=false;
        },
        //删除商品
        delete_pro:function(id){
            var that=this;
            that.list = that.list.filter((item) => {
                return item.ID != id
                });
        },
        //保存活动
        do_saveProduct:function(){
            var that=this;
            
            if (that.Name == '') {
                api.toast({
                    msg: '请输入活动名称',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            


            if (that.ProjectName == '') {
                api.toast({
                    msg: '请选择所属项目',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if  ( that.Type==1&&that.Discount == '') {
                api.toast({
                    msg: '请输入折扣比例',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            var reg=/0\.[1-9]\d|1\.00/;
            var result=reg.test(that.Discount);
            if(!result){
                api.toast({
                    msg: '折扣格式不正确',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
           
            if (that.youxiaoTime == '') {
                api.toast({
                    msg: '请输入有效期',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.list.length<=0) {
                api.toast({
                    msg: '至少选择一件商品',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            var option={};
            if(that.ID){
                option.ID=that.ID;
            }else{
                option.ID=0;
            }
            option.Name=that.Name;
            option.ProjectID=(that.ProjectID).toString();
            option.ProjectName=that.ProjectName;
            option.Type=that.Type;
            option.Discount=that.Discount;
            option.StartTime=that.StartTime;
            option.EndTime=that.EndTime;
            option.Products=that.list;
            option.action='savebusinesssaleactivity';
            console.log(JSON.stringify(option));
            ns.post(option, function(succeed, data, err) {
                console.log(JSON.stringify(err));
                console.log(JSON.stringify(data));
                if(succeed){
                    if(data.status){
                        api.toast({
                            msg: '保存成功',
                            duration: 2000,
                            location: 'bottom'
                        }); 
                        api.sendEvent({
                            name:'load_active'
                        })
                        api.closeWin();
                    }else{
                        api.toast({
                            msg: data.msg,
                            duration: 2000,
                            location: 'bottom'
                        }); 
                    }
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    }); 
                }
            });

            
        },
        // 选择有效时间
        choose_time(){
            this.showTime=true;
        },
        onConfirm(date) {
            const [start, end] = date;
            this.youxiaoTime = this.formatDate(start) +'至'+ this.formatDate(end);
            this.StartTime=this.formatDate(start);
            this.EndTime=this.formatDate(end);
            this.showTime = false;
          },
          formatDate(day){
            var y = day.getFullYear();
            var m = day.getMonth() + 1;
            var d = day.getDate();
            var nowdate = y + '-' + (m >= 10 ? m : '0' + m) + '-' + (d >= 10 ? d : '0' + d);
            return nowdate;
        },
    }
       


      
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    // toast = new auiToast();
    // actionsheet = new auiActionsheet();
    UIAlbumBrowser = api.require('UIAlbumBrowser');
    app.ID = api.pageParam.id;
    if(app.ID){
        app.Name=api.pageParam.SaleActivityInfo.Name;
        app.ProjectID=api.pageParam.SaleActivityInfo.ProjectIDList;
        app.ProjectName=api.pageParam.SaleActivityInfo.ProjectNameList;
        app.Type=api.pageParam.SaleActivityInfo.Type;
        app.Discount=api.pageParam.SaleActivityInfo.Discount;
        app.StartTime=api.pageParam.SaleActivityInfo.ValidStartTime;
        app.EndTime=api.pageParam.SaleActivityInfo.ValidEndTime;
        app.youxiaoTime = app.StartTime +'至'+ app.EndTime;
        app.list=api.pageParam.ProductList;
    }
    api.addEventListener({
        name: 'do_add_product'
    }, function (ret, err) {
        console.log(JSON.stringify(ret.value.list));
        app.list = ret.value.list;
    });
    app.get_project();
    

};
